#include "stm32f10x.h"

/**
 * @brief Initialize TIM2 to encoder interface mode
 * @param NULL
 * @retval NULL
*/
void Encoder_TIM2_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);   // 开启时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//	GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE);
//	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   // 浮空输入
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;	// PA0, PA1
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		// IO口速率
	GPIO_Init(GPIOA, &GPIO_InitStructure);					// GPIO初始化
	
	TIM_InternalClockConfig(TIM2);
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure);				// 初始化时基单元默认值
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;	// 向上计数
	TIM_TimeBaseInitStructure.TIM_Period = 65535;			//ARR 设置下一个更新活动的自动重装载寄存器
	TIM_TimeBaseInitStructure.TIM_Prescaler = 0x0;		//PSC 预分频
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;	// 时钟分割
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);		// 时基单元初始化
    TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
    TIM_ICInitTypeDef TIM_ICInitStructure;
    TIM_ICStructInit(&TIM_ICInitStructure);
    TIM_ICInitStructure.TIM_ICFilter = 10;          // 滤波10
    TIM_ICInit(TIM2, &TIM_ICInitStructure);
    TIM_ClearFlag(TIM2, TIM_FLAG_Update);           // 清除TIM的更新标志位
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

	TIM_SetCounter(TIM2, 0);
    TIM_Cmd(TIM2, ENABLE);							// 启动定时器2
}


/**
 * @brief Initialize TIM3 to encoder interface mode
 * @param NULL
 * @retval NULL
*/
void Encoder_TIM4_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
	
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//	GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE);
//	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   // 浮空输入
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;	// PB6, PB7
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		// IO口速率
	GPIO_Init(GPIOB, &GPIO_InitStructure);					// GPIO初始化
	
	TIM_InternalClockConfig(TIM4);
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure);				// 初始化时基单元默认值
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;	// 向上计数
	TIM_TimeBaseInitStructure.TIM_Period = 65535;			//ARR 设置下一个更新活动的自动重装载寄存器
	TIM_TimeBaseInitStructure.TIM_Prescaler = 0x0;		    //PSC 预分频
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;	// 时钟分割
	TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure);		// 时基单元初始化
    TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
    TIM_ICInitTypeDef TIM_ICInitStructure;
    TIM_ICStructInit(&TIM_ICInitStructure);
    TIM_ICInitStructure.TIM_ICFilter = 10;          // 滤波10
    TIM_ICInit(TIM4, &TIM_ICInitStructure);
    TIM_ClearFlag(TIM4, TIM_FLAG_Update);           // 清除TIM的更新标志位
    TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);

	TIM_SetCounter(TIM4, 0);
    TIM_Cmd(TIM4, ENABLE);							// 启动定时器4
}


/**
 * @brief Read encoder count per unit time
 * @param TIMX: Timer
 * @retval Encoder: Speed
*/
int32_t Read_Encoder(uint8_t TIMX)
{
    int16_t Encoder_TIM;
    switch (TIMX)
    {
    case 2: Encoder_TIM = (long)TIM2 -> CNT; TIM2 -> CNT = 0; break;
    case 4: Encoder_TIM = (long)TIM4 -> CNT; TIM4 -> CNT = 0; break;
    default: Encoder_TIM = 0; break;
    }
    return Encoder_TIM;
}


/**
 * @brief TIM4 interrupt service function
 * @param NULL
 * @retval NULL
*/
void TIM4_IRQHandler(void)
{
    if (TIM4 -> SR & 0x0001)      // 溢出中断
    {}
    TIM4 -> SR &= ~(1<<0);       // 清除中断标志位
}

/**
 * @brief TIM4 interrupt service function
 * @param NULL
 * @retval NULL
*/
void TIM2_IRQHandler(void)
{
    if (TIM2 -> SR & 0x0001)      // 溢出中断
    {}
    TIM2 -> SR &= ~(1<<0);       // 清除中断标志位
}